<?php
/**
 * 固定模块缓存数据发布
 * @author gemj
 * @copyright (c) 2011-04-04
 * @version v1.0
 */

class Auto_Resume_Page_Module extends Auto_Resume_Page_Abstract
{
	
	protected $_cachePool = array();

	/**
	 *
	 * @var ME_DAL_RefreshCacheLoader
	 */
	protected static $_cacheLoader;
	/**
	 * ��ģ��
	 */
	protected $_modules = array(
	'Resume_ModuleData'		    => '各固定模块缓存数据'
	);


	public function validate(ME_Request $input)
	{
		$moduleName = $input->get('m');#ģ����
		$fromId     = (int)$input->get('f');#��ʼID
		$toId       = (int)$input->get('t');#����ID ������
		$recursion  = $input->get('r');#�Ƿ�ȫ������ģ��
		$latest     = (bool)$input->get('l');#�Ƿ񷢲�����
		$whole      = (bool)$input->get('w');#�Ƿ񷢲�����

		if ($moduleName && !array_key_exists($moduleName, $this->_modules)) {
			self::output("The module \"{$moduleName}\" is not exists!");
		}

		if (!$moduleName && !$recursion) {
			self::output("ModuleName is empty!", true);
		}

		$input->moduleName = $moduleName;
		$input->recursion  = $recursion;
		$input->fromId     = $fromId;
		$input->toId       = $toId;
		$input->latest     = $latest;
		$input->whole      = $whole;

		return true;
	}

	/**
	 *
	 */
	public function doDefault(ME_Request $input)
	{
		if ($input->moduleName) {
			$BeginTime = date("Y-m-d H:i:s");
			self::output("{$input->moduleName} begin:{$BeginTime}", false);
			$this->iterator($input);
			$EndTime = date("Y-m-d H:i:s");
			self::output("{$input->moduleName} succeed!{$EndTime}", true);
		} else {
			if(!$input->recursion){
				self::output("ModuleName is emply!", true);
			} else { //����r����ʱ����������ģ��
				foreach ($this->_modules as $moduleName => $modueIntro) {
					$input->moduleName = $moduleName;
					$BeginTime = date("Y-m-d H:i:s");
					self::output("{$moduleName} begin:{$BeginTime}", false);
					$this->iterator($input);
					$EndTime = date("Y-m-d H:i:s");
					self::output("{$modueIntro}\t\tsucceed!{$EndTime}", false);
				}
			}
		}
		exit;
	}

	/**
	 * ��Ʒ�����
	 */
	public function iterator(ME_Request $input)
	{
		//print_r($input);
		$moduleName = $input->moduleName;

		if (!$moduleName) {
			self::output('ModuleName is emply!', true);
		}

		if($input->whole){ //����ȫ��
			$glyIdArr = Libs_Product_Glossary::getAllGlossaryIds();
		} else { //���?������
			$glyIdArr = Libs_Product_Glossary::getConditionGlossaryIds(array('fromId'=>$input->fromId,'toId'=>$input->toId));
		}
		//ѭ������������з���
		if(!empty($glyIdArr) && is_array($glyIdArr)){
			foreach ($glyIdArr as $val) {
				$input->glyId = is_array($val) ?  $val['id'] : (int)$val;
				$this->publisher($input);
			}
		} else {
			self::output('data is empty!');
		}
	}


	/**
	 * ��������ģ��
	 */
	private function publisher(ME_Request $input)
	{
		$moduleName = $input->moduleName;
		$glyId = $input->glyId;
		//�ÿ�ԭ��
		$input->glyId = 0;

		if(!$glyId || !$moduleName){
			self::output('error!', true);
		}

		$param = array('glyId' => $glyId);

		if (!$moduleName) {
			self::output('error!', true);
		}
		if (self::setCache($moduleName, $param)) {
			self::output("\t{$moduleName}\t{".$glyId."}\tsucceed!", false);
		} else {
			self::output("\t{$moduleName}\t{".$glyId."}\tfailed", false);
		}
	}

}